#!/usr/bin/perl -w

#created by Menny Even-Danan (mennyed at the g mail thing)
#Contains a scrapping code for movies from IMDB.COM
#(November 17th 2009) This code was originally used by the main script for scraping, but since it is illegal to scrap IMDB (terms of use)
#the code was removed.

my $cached_imdb_movie_id = "";
my ($cached_imdb_title_url, $cached_imdb_title, $cached_imdb_plot, $cached_imdb_director, $cached_imdb_release_date, $cached_imdb_rating, $cached_imdb_poster, $cached_imdb_fanart, $cached_motechnet_poster, $cached_content_rating);
sub get_movie_meta_data_from_imdb_id
{
	my $imdb_id = $_[0];
	if ($imdb_id ne $cached_imdb_movie_id)
	{
		#http://www.imdb.com/title/tt0465502/
		$cached_imdb_title_url = "http://www.imdb.com/title/tt".$imdb_id."/";
		my $movie_page = get_http_response($cached_imdb_title_url);
		#<title>Igor (2008)</title>
		($cached_imdb_title) = $movie_page =~ m/<title>(.*?)<\/title>/i;
		my $motech_id = $cached_imdb_title;
		#<div class="photo">
		#<a name="poster" href="/rg/action-box-title/primary-photo/media/rm3684799488/tt0465502" title="Igor"><img border="0" alt="Igor" title="Igor" src="http://ia.media-imdb.com/images/M/MV5BNDYwNjM1ODkxOV5BMl5BanBnXkFtZTcwMTQyMzI5MQ@@._V1._SX90_SY140_.jpg" /></a>
		#</div>
		($cached_imdb_poster) = $movie_page =~ m/<div class="photo">.*?<img[^<>]*?src="([^<>]+?)"[^<>]*?\/>/i;
		#new poster URL: http://www.motechposters.com/posters/igor-2008_poster.jpg
		#needs to spoof referrer: http://www.motechposters.com/title/igor-2008/
		#removing special html escape codes
		$motech_id =~ s/&#\d{1,8}?;//gi;
		$motech_id =~ s/&#x\d{1,8}?;//gi;
		$motech_id =~ s/[\/()'&!,.:]//g; #replaces all "/()'&!,.:" with ""
		$motech_id = lc($motech_id); #forms all lowercase
		$motech_id =~ s/[^a-z \-\d]//g; #removes all non-alphanumeric/non-space characters
		$motech_id =~ s/[ ][ ]+/ /g; #replaces all multiple spaces with single space
		$motech_id =~ s/ /-/g; #replaces " " with "-"
		$cached_motechnet_poster = "http://www.motechposters.com/posters/".$motech_id."_poster.jpg";
		if (!$cached_imdb_poster) {$cached_imdb_poster = "";}
		if ($cached_imdb_poster =~ m/noposter/i) { $cached_imdb_poster = ""; }
		($cached_imdb_rating) = $movie_page =~ m/<b>(\d+.\d+)\/10<\/b>/;
		if (!$cached_imdb_rating) { $cached_imdb_rating = 0; }
		#<h5>Director:</h5>
		#<a href="/name/nm1902232/">Anthony Leondis</a><br/>
		#</div>
		($cached_imdb_director) = $movie_page =~ m/<div.*?Director[s]?:.*?<a href.*?>(.*?)<\/a>/;
		if (!$cached_imdb_director) {$cached_imdb_director = "";}
		($cached_imdb_release_date) = $movie_page =~ m/<div.*?Release Date:.*?>.*?(\d{4}).*?</;
		if (!$cached_imdb_release_date) {$cached_imdb_release_date = "";}
		($cached_imdb_plot) = $movie_page =~ m/<div.*?Plot:.*?>\s*(.*?)\s*</;
		if (!$cached_imdb_plot) {$cached_imdb_plot = "-";}
		($cached_content_rating) = $movie_page =~ m/<h5.*?Certification:.*?USA:\s*(.*?)\s*&&.*?</;
		if (!$cached_content_rating) {$cached_content_rating = "NR";}
		$cached_imdb_movie_id = $imdb_id;
	}
	
	return ($cached_imdb_movie_id, $cached_imdb_title_url, $cached_imdb_title, $cached_imdb_plot, $cached_imdb_director, $cached_imdb_release_date, $cached_imdb_poster, $cached_imdb_rating, $cached_motechnet_poster, $cached_content_rating);
}

sub parse_imdb_table
{
	my $movies_table = $_[0];
	my $movie_to_search = $_[1];
	my $best_similarity = $_[2];
	my $filenameYear = $_[3];
	my $best_ID_till_now = "";

	if ($movies_table)
	{
		while ($movies_table =~ m/<a href="\/title\/tt(\d+)\/[^<]*>([^<]+?)<\/a>\s*([^<]*?)\s*</gi)
		{
			my $id = $1;
			my $name = $2;
			my $post_fix = $3;
			#video game? no need
			if ($post_fix =~ m/(VG)/){next;	}

			print "Movie '$name' with ID: $id\n";

			# if we were passed a year from the filename then use it with the comparisons
			my $fileMovieName;
			my $imdbMovieName;
			if (length($filenameYear) > 0) {
				$fileMovieName = $movie_to_search . " ($filenameYear)";
				$imdbMovieName = $name . " " . $post_fix;
			} else {
                                $fileMovieName = $movie_to_search;
                                $imdbMovieName = $name;
			}
#print "filenameYear: $filenameYear fileMovieName: $fileMovieName \nimdbMovieName: $imdbMovieName\n";
	
			my $current_similarity = levenshtein($fileMovieName, $imdbMovieName);

			# Make the movie name in the format "Movie name (year released)"
			$name .= " ".$post_fix;

#print "$movie_to_search, $name = " . $current_similarity . "\n";

			if ($current_similarity < $best_similarity)
			{
				if ($best_similarity eq 10000000){print "Found a possible match for '$movie_to_search' as '$name' (ID $id)\n";}
				else {print "Found a better possible match for '$movie_to_search' as '$name' (ID $id)\n";}
				$best_similarity = $current_similarity;
				$best_ID_till_now = "".$id;
			}
		}
	}
	return ($best_ID_till_now, $best_similarity);
}

my $cached_imdb_movie_name = "";
my $cached_imdb_ID = "";
sub get_imdb_ID_for_movie_name
{
	my $movie_name = $_[0];
	my $releaseYear = $_[1];

	if ($movie_name ne $cached_imdb_movie_name)
	{
		$cached_imdb_movie_name = $movie_name;
		my $imdb_response = get_http_response("http://www.imdb.com/find?s=tt&q=".$movie_name);
		#I could either been directed to a results page, or the movie page.
		#<link rel="canonical" href="http://www.imdb.com/find?
		#<link rel="canonical" href="http://www.imdb.com/title/tt
		if ($imdb_response =~ m/<link rel="canonical" href="http:\/\/www.imdb.com\/title\/tt(\d+)\//i)
		{#exact match, no way other!
			$cached_imdb_ID = $1;
		}
		else
		{
			$imdb_response =~ m/Popular Titles.*?Displaying (\d+) Result.*?<table>(.*?)<\/table>/i;
			my ($pop_results_count, $pop_match_movies) = $imdb_response =~ m/Popular Titles.*?Displaying (\d+) Result.*?<table>(.*?)<\/table>/i;
			#<p><b>Titles (Exact Matches)</b> (Displaying 4 Results)<table><tr> <td valign="top"><a href="/title/tt0465502/" onClick="(new Image()).src='/rg/find-tiny-photo-1/title_exact/images/b.gif?link=/title/tt0465502/';"><img src="http://ia.media-imdb.com/images/M/MV5BNDYwNjM1ODkxOV5BMl5BanBnXkFtZTcwMTQyMzI5MQ@@._V1._SY30_SX23_.jpg" width="23" height="32" border="0"></a>&nbsp;</td><td align="right" valign="top"><img src="/images/b.gif" width="1" height="6"><br>1.</td><td valign="top"><img src="/images/b.gif" width="1" height="6"><br><a href="/title/tt0465502/" onclick="(new Image()).src='/rg/find-title-1/title_exact/images/b.gif?link=/title/tt0465502/';">Igor</a> (2008)     </td></tr><tr> <td valign="top"><img src="/images/b.gif" alt="" width="23" height="1"></td><td align="right" valign="top">2.</td><td valign="top"><a href="/title/tt0359451/" onclick="(new Image()).src='/rg/find-title-2/title_exact/images/b.gif?link=/title/tt0359451/';">Igor</a> (1998)     </td></tr><tr> <td valign="top"><img src="/images/b.gif" alt="" width="23" height="1"></td><td align="right" valign="top">3.</td><td valign="top"><a href="/title/tt1156061/" onclick="(new Image()).src='/rg/find-title-3/title_exact/images/b.gif?link=/title/tt1156061/';">Igor</a> (2000)     </td></tr><tr> <td valign="top"><img src="/images/b.gif" alt="" width="23" height="1"></td><td align="right" valign="top">4.</td><td valign="top"><a href="/title/tt1153092/" onclick="(new Image()).src='/rg/find-title-4/title_exact/images/b.gif?link=/title/tt1153092/';">Igor</a> (2008) (VG)     </td></tr></table> </p> <p><b>Titles (Partial Matches)</b> (Displaying 16 Results)<table><tr> <td valign="top"><a href="/title/tt0832958/" onClick="(new Image()).src='/rg/find-tiny-photo-1/title_substring/images/b.gif?link=/title/tt0832958/';"><img src="http://ia.media-imdb.com/images/M/MV5BMTI2Nzc3Mzk4MF5BMl5BanBnXkFtZTcwNzQzNjczMQ@@._V1._SY30_SX23_.jpg" width="23" height="32" border="0"></a>&nbsp;</td><td align="right" valign="top"><img src="/images/b.gif" width="1" height="6"><br>1.</td><td valign="top"><img src="/images/b.gif" width="1" height="6"><br><a href="/title/tt0832958/" onclick="(new Image()).src='/rg/find-title-1/title_substring/images/b.gif?link=/title/tt0832958/';">Sur la trace d&#x27;Igor Rizzi</a> (2006)    <br>&#160;aka <em>"On the Trail of Igor Rizzi"</em> - <em>(English title)</em> </td></tr><tr> <td valign="top"><img src="/images/b.gif" alt="" width="23" height="1"></td><td align="right" valign="top">2.</td><td valign="top"><a href="/title/tt0362752/" onclick="(new Image()).src='/rg/find-title-2/title_substring/images/b.gif?link=/title/tt0362752/';">Igor Stravinsky: Composer</a> (2001) (TV)     </td></tr><tr> <td valign="top"><a href="/title/tt0089326/" onClick="(new Image()).src='/rg/find-tiny-photo-3/title_substring/images/b.gif?link=/title/tt0089326/';"><img src="http://ia.media-imdb.com/images/M/MV5BMjEzNDc4ODc5Nl5BMl5BanBnXkFtZTcwNzAyNjIyMQ@@._V1._SY30_SX23_.jpg" width="23" height="32" border="0"></a>&nbsp;</td><td align="right" valign="top"><img src="/images/b.gif" width="1" height="6"><br>3.</td><td valign="top"><img src="/images/b.gif" width="1" height="6"><br><a href="/title/tt0089326/" onclick="(new Image()).src='/rg/find-title-3/title_substring/images/b.gif?link=/title/tt0089326/';">Igor and the Lunatics</a> (1985)     </td></tr><tr> <td valign="top"><a href="/title/tt0170115/" onClick="(new Image()).src='/rg/find-tiny-photo-4/title_substring/images/b.gif?link=/title/tt0170115/';"><img src="http://ia.media-imdb.com/images/M/MV5BMTQ0ODIwNTAxMV5BMl5BanBnXkFtZTcwODc2MTgxMQ@@._V1._SY30_SX23_.jpg" width="23" height="32" border="0"></a>&nbsp;</td><td align="right" valign="top"><img src="/images/b.gif" width="1" height="6"><br>4.</td><td valign="top"><img src="/images/b.gif" width="1" height="6"><br><a href="/title/tt0170115/" onclick="(new Image()).src='/rg/find-title-4/title_substring/images/b.gif?link=/title/tt0170115/';">Knyaz Igor</a> (1970)    <br>&#160;aka <em>"Prince Igor"</em> - Canada <em>(English title)</em> <em>(literal title)</em> </td></tr><tr> <td valign="top"><img src="/images/b.gif" alt="" width="23" height="1"></td><td align="right" valign="top">5.</td><td valign="top"><a href="/title/tt0254714/" onclick="(new Image()).src='/rg/find-title-5/title_substring/images/b.gif?link=/title/tt0254714/';">Prince Igor</a> (1990) (TV)     </td></tr><tr> <td valign="top"><img src="/images/b.gif" alt="" width="23" height="1"></td><td align="right" valign="top">6.</td><td valign="top"><a href="/title/tt0400914/" onclick="(new Image()).src='/rg/find-title-6/title_substring/images/b.gif?link=/title/tt0400914/';">Valentin Katajews chirurgische Eingriffe in das Seelenleben des Dr. Igor Igorowitsch</a> (1967) (TV)     </td></tr><tr> <td valign="top"><a href="/title/tt0337510/" onClick="(new Image()).src='/rg/find-tiny-photo-7/title_substring/images/b.gif?link=/title/tt0337510/';"><img src="http://ia.media-imdb.com/images/M/MV5BMjE4NDgxMTkwMF5BMl5BanBnXkFtZTcwMTU1NzYyMQ@@._V1._SY30_SX23_.jpg" width="23" height="32" border="0"></a>&nbsp;</td><td align="right" valign="top"><img src="/images/b.gif" width="1" height="6"><br>7.</td><td valign="top"><img src="/images/b.gif" width="1" height="6"><br><a href="/title/tt0337510/" onclick="(new Image()).src='/rg/find-title-7/title_substring/images/b.gif?link=/title/tt0337510/';">Young People&#x27;s Concerts: Happy Birthday, Igor Stravinsky</a> (1962) (TV)     </td></tr><tr> <td valign="top"><img src="/images/b.gif" alt="" width="23" height="1"></td><td align="right" valign="top">8.</td><td valign="top"><a href="/title/tt1023441/" onclick="(new Image()).src='/rg/find-title-8/title_substring/images/b.gif?link=/title/tt1023441/';">Coco Chanel &#x26; Igor Stravinsky</a> (2009)    <br>&#160;aka <em>"Coco &#x26; Igor"</em> - France <em>(working title)</em> </td></tr><tr> <td valign="top"><img src="/images/b.gif" alt="" width="23" height="1"></td><td align="right" valign="top">9.</td><td valign="top"><a href="/title/tt0474661/" onclick="(new Image()).src='/rg/find-title-9/title_substring/images/b.gif?link=/title/tt0474661/';">Extra&#xF1;o caso de Igor Molina, El</a> (2002)     </td></tr><tr> <td valign="top"><img src="/images/b.gif" alt="" width="23" height="1"></td><td align="right" valign="top">10.</td><td valign="top"><a href="/title/tt0456989/" onclick="(new Image()).src='/rg/find-title-10/title_substring/images/b.gif?link=/title/tt0456989/';">I galebovi su se smijali</a> (2005) (TV)    <br>&#160;aka <em>"I galebovi su se smijali. Olimpijske price - Igor Boraska"</em> - Croatia <em>(long title)</em> </td></tr><tr> <td valign="top"><img src="/images/b.gif" alt="" width="23" height="1"></td><td align="right" valign="top">11.</td><td valign="top"><a href="/title/tt0306336/" onclick="(new Image()).src='/rg/find-title-11/title_substring/images/b.gif?link=/title/tt0306336/';">&#x22;The Igor Cassini Show&#x22;</a> (1953) <small>(TV series)</small>     </td></tr><tr> <td valign="top"><img src="/images/b.gif" alt="" width="23" height="1"></td><td align="right" valign="top">12.</td><td valign="top"><a href="/title/tt1357427/" onclick="(new Image()).src='/rg/find-title-12/title_substring/images/b.gif?link=/title/tt1357427/';">Igor &#x26; Mal</a> (2008)     </td></tr><tr> <td valign="top"><img src="/images/b.gif" alt="" width="23" height="1"></td><td align="right" valign="top">13.</td><td valign="top"><a href="/title/tt0090456/" onclick="(new Image()).src='/rg/find-title-13/title_substring/images/b.gif?link=/title/tt0090456/';">Igor Savvovich</a> (1986) (TV)     </td></tr><tr> <td valign="top"><img src="/images/b.gif" alt="" width="23" height="1"></td><td align="right" valign="top">14.</td><td valign="top"><a href="/title/tt0340772/" onclick="(new Image()).src='/rg/find-title-14/title_substring/images/b.gif?link=/title/tt0340772/';">Igor: The Paris Years</a> (1982)    <br>&#160;aka <em>"Igor Stravinsky: The Paris Years Chez Pleyel 1920-1929"</em> - USA </td></tr><tr> <td valign="top"><img src="/images/b.gif" alt="" width="23" height="1"></td><td align="right" valign="top">15.</td><td valign="top"><a href="/title/tt0289261/" onclick="(new Image()).src='/rg/find-title-15/title_substring/images/b.gif?link=/title/tt0289261/';">Knyaz Igor</a> (1981) (V)     </td></tr><tr> <td valign="top"><img src="/images/b.gif" alt="" width="23" height="1"></td><td align="right" valign="top">16.</td><td valign="top"><a href="/title/tt0360476/" onclick="(new Image()).src='/rg/find-title-16/title_substring/images/b.gif?link=/title/tt0360476/';">Polovetzian Dances from Prince Igor</a> (1954)    <br>&#160;aka <em>"CinemaScope Specials (#5): Polovetzian Dances from Prince Igor"</em> - USA <em>(series title)</em> </td></tr></table> </p>
			my ($exact_results_count, $exact_match_movies) = $imdb_response =~ m/Exact Matches.*?Displaying (\d+) Result.*?<table>(.*?)<\/table>/i;
			#Titles (Approx Matches) (Displaying 2 Results)
			if ($exact_match_movies || $pop_match_movies)
			{
				my ($imdb_id1, $similarity1) = parse_imdb_table($pop_match_movies, $movie_name, 10000000, $releaseYear);
				my ($imdb_id2, $similarity2) = parse_imdb_table($exact_match_movies, $movie_name, $similarity1, $releaseYear);

				if ($similarity1 > $similarity2){
					$cached_imdb_ID =  $imdb_id2;
				} else {
					$cached_imdb_ID = $imdb_id1;
				}
			}
			else
			{
				#no matches, I'll take the approx and/or partial
				my ($partial_results_count, $partial_match_movies) = $imdb_response =~ m/Partial Matches.*?Displaying (\d+) Result.*?<table>(.*?)<\/table>/i;	# LAURIE ADD
				my ($imdb_id_partial, $similarity_partial) = parse_imdb_table($partial_match_movies, $movie_name, 10000000, $releaseYear);					# LAURIE ADD


				my ($approx_results_count, $approx_match_movies) = $imdb_response =~ m/Approx Matches.*?Displaying (\d+) Result.*?<table>(.*?)<\/table>/i;
				my ($imdb_id_approx, $similarity_approx) = parse_imdb_table($approx_match_movies, $movie_name, 10000000, $releaseYear);

				# If the partial match was a better match than the approx match then use the partial match
				if ($similarity_partial < $similarity_approx) {	# LAURIE ADD
					$cached_imdb_ID = $imdb_id_partial;	# LAURIE ADD
				} elsif ($imdb_id_approx) {		
					$cached_imdb_ID = $imdb_id_approx;
				} else {
					$cached_imdb_ID = "";
				}
			}
		}
	}
	return $cached_imdb_ID;
}
